package cn.wizzer.app.web.commons.ext.websocket.action.user;

import cn.wizzer.app.sys.modules.services.SysParamService;
import cn.wizzer.app.sys.modules.services.SysSensitiveService;
import cn.wizzer.app.user.modules.models.UserChatMsg;
import cn.wizzer.app.user.modules.models.enums.MsgTypeEnum;
import cn.wizzer.app.user.modules.models.vo.ChatUserVO;
import cn.wizzer.app.user.modules.services.UserChatMsgService;
import cn.wizzer.app.web.commons.ext.websocket.action.attendant.OpenAction;
import cn.wizzer.app.web.commons.ext.websocket.filter.TokenFilter;
import cn.wizzer.app.web.commons.ext.websocket.main.action.BaseNettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.action.NettyAction;
import cn.wizzer.app.web.commons.ext.websocket.main.base.NettyCache;
import cn.wizzer.app.web.commons.ext.websocket.main.result.ChatResult;
import cn.wizzer.app.web.commons.utils.NettyMsgUtil;
import cn.wizzer.framework.util.Result;
import cn.wizzer.framework.util.JSONUtil;
import cn.wizzer.framework.util.ThreadUtil;
import cn.wizzer.framework.util.param.Param4MapVerify;
import cn.wizzer.framework.util.param.ParamVerify;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import io.netty.channel.Channel;
import org.apache.commons.lang3.EnumUtils;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;


@IocBean
@NettyAction(uri = "/user/send", filters = {TokenFilter.class})
public class UserSendAction extends BaseNettyAction {

    @Inject
    @Reference
    private UserChatMsgService chatMsgService;
    @Inject
    @Reference
    private SysSensitiveService sysSensitiveService;
    @Inject
    private NettyMsgUtil nettyMsgUtil;

    @Inject
    @Reference
    private SysParamService paramService;

    @Override
    public Result action(Channel income,
                         @Param4MapVerify(fields = {@ParamVerify(name = "content", required = true),
                                 @ParamVerify(name = "type", required = true)}) JSONObject params) {
        ChatUserVO from = NettyCache.globalGet(params.get("appid"));
        ChatUserVO targetAttendant = NettyCache.globalGet(NettyCache.localGet(income, "targetAttendant"));
        //客服异常掉线--重新分配
        if (targetAttendant == null) {
            BaseNettyAction.instance(OpenAction.class).action(income, params);
            targetAttendant = NettyCache.globalGet(NettyCache.localGet(income, "targetAttendant"));
        }
        if (targetAttendant != null) {
            //客服异常重连处理--重新加入到原客服会话列表
            if (!targetAttendant.getUsers().contains(from.getId())) {
                targetAttendant.getUsers().add(from.getId());
                //通知刷新用户列表
                nettyMsgUtil.send(targetAttendant.getChannel(), ChatResult.MessageTypeEnum.ACTION, ChatResult.ActionEnum.JOIN, from, null);
            }
        }

        UserChatMsg msg = new UserChatMsg();
        msg.setContent(sysSensitiveService.filter(params.getString("content")));
        msg.setCtAt(System.currentTimeMillis());
        msg.setToUser(targetAttendant == null ? null : targetAttendant.getId());
        msg.setUserId(from.getId());
        msg.setType(EnumUtils.getEnum(MsgTypeEnum.class, params.getString("type")));

        msg = chatMsgService.insert(msg);

        if (targetAttendant != null) {
            //发给客服
            nettyMsgUtil.send(targetAttendant.getChannel(), ChatResult.MessageTypeEnum.USER, ChatResult.ActionEnum.SEND, from, JSONUtil.toJSONString(msg));
        }
        //发给自己
        nettyMsgUtil.send(income, ChatResult.MessageTypeEnum.USER, ChatResult.ActionEnum.SEND, from, JSONUtil.toJSONString(msg));
        return Result.success(msg);
    }
}
